package com.sky.mediator;

/**
 * @author : wushikai
 * <p>
 * date : 2020-08-19
 */



import java.util.ArrayList;
import java.util.List;

/**
 * 中介者模式实例, 链家房屋中介网
 */


public abstract class ApartmentIntermediary {


    String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    abstract void registerStudent(Student student);  //注册大学毕业生

    abstract void registerLandlord(Landlord landlord); //注册房东

    abstract void relay2Students(  Landlord landlord);  //发送给学生

    abstract void relay2Landlords(Student student ); //发送给房东
}

/**
 * 租房中介--> 链家
 */
class Lianjia extends ApartmentIntermediary {

    List<Student> students = new ArrayList<>();
    List<Landlord> landlords = new ArrayList<>();

    @Override
    void registerStudent(Student student) {
        if (!students.contains(student)){
            students.add(student);
            student.setApartmentIntermediary(this);
        }
    }

    @Override
    void registerLandlord(Landlord landlord) {
        if (!landlords.contains(landlord)){
            landlords.add(landlord);
            landlord.setApartmentIntermediary(this);
        }
    }

    @Override
    void relay2Students(Landlord  l ) {
        for (Landlord landlord : landlords){

            if (!landlord.equals(l)){
                landlord.receive();
            }
        }
    }

    @Override
    void relay2Landlords(  Student s){



        for (Student student : students){
            if (!student.equals(s)){
                student.receive();
            }

        }
    }
}


abstract class Student {

    ApartmentIntermediary apartmentIntermediary;

    String name;


    //找中介平台 , 把自己注册进去
    public void setApartmentIntermediary(ApartmentIntermediary apartmentIntermediary) {
        this.apartmentIntermediary = apartmentIntermediary;
    }

    //接收平台的房源消息
    public abstract void receive();

    //发送自己要租房的消息
    public abstract void send();
}


abstract class Landlord {

    String name;

    ApartmentIntermediary apartmentIntermediary;
    //找中介平台 , 把自己注册进去
    public void setApartmentIntermediary(ApartmentIntermediary apartmentIntermediary) {
        this.apartmentIntermediary = apartmentIntermediary;
    }

    //接收平台的住房请求的消息
    public abstract void receive();

    //发送自己要出租房子的消息
    public abstract void send();

}


//清华的学生
class StudentQinghua extends Student{

    public StudentQinghua() {
    }
    public StudentQinghua(String name) {
        super.name= name;
    }

    String getName(){
        return super.name;
    }

    @Override
    public void receive() {
        System.out.println(getName() + "收到" + super.apartmentIntermediary.msg);
    }

    @Override
    public void send() {
        System.out.println(getName() + "想租房");
        super.apartmentIntermediary.relay2Landlords(this);
    }
}





//北大的学生
class StudentBeiDa extends Student{



    public StudentBeiDa() {
    }

    public StudentBeiDa(String name ) {
        super.name= name;
    }


    String getName(){
        return super.name;
    }

    @Override
    public void receive() {
        System.out.println(getName() + "收到" + super.apartmentIntermediary.msg);
    }

    @Override
    public void send() {
        System.out.println(getName() + "想租房");
        super.apartmentIntermediary.relay2Landlords(this);
    }
}


//昌平区的房东
class LandlordChangping extends Landlord {


    public LandlordChangping() {

    }

    public LandlordChangping(String name) {

        super.name = name;
    }


    String getName(){
        return super.name;
    }


    @Override
    public void receive() {
        System.out.println(getName() + "收到" + super.apartmentIntermediary.msg);
    }

    @Override
    public void send() {
        System.out.println(getName() + "想出租房子, 价格3k/月");

    }
}


//顺义区的房东
class LandlordShunyi extends Landlord {


    public LandlordShunyi() {
    }


    public LandlordShunyi(String name) {

        super.name = name;
    }

    String getName(){
        return super.name;
    }


    @Override
    public void receive() {
        System.out.println(getName() + "收到" + super.apartmentIntermediary.msg);
    }

    @Override
    public void send() {
        System.out.println(getName() + "想出租房子, 价格4k/月");

    }
}




class Web{


    public static void main(String[] args){

        ApartmentIntermediary lianjia  = new Lianjia();

        Student s1, s2 ;
        s1 =  new  StudentQinghua("清华");
        s2 =  new StudentBeiDa("北大");

        Landlord l1, l2 ;

        l1 = new LandlordChangping("昌平");
        l2 = new LandlordShunyi("顺义");

        lianjia.registerStudent(s1);
        lianjia.registerStudent(s2);

        lianjia.registerLandlord(l1);
        lianjia.registerLandlord(l2);

        s1.send();

    }
}






